
package com.be.beadmin.quality.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.quality.dto.input.ProcessInspectionItemRelationDto;
import com.be.beadmin.quality.dto.output.ProcessInspectionItemRelationEx;
import com.be.beadmin.quality.entity.ProcessInspectionItemRelation;
import com.be.beadmin.quality.entity.ProcessInspectionItemRelation_;
import com.be.beadmin.quality.mapper.ProcessInspectionItemRelationMapper;
import com.be.beadmin.quality.mapper.ProcessInspectionItemRelationMapperEx;
import com.be.beadmin.quality.repository.ProcessInspectionItemRelationRepository;
import com.be.beadmin.quality.service.ProcessInspectionItemRelationService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zjm
* @date 2023-05-28
**/
@Service
@RequiredArgsConstructor
public class ProcessInspectionItemRelationServiceImpl implements ProcessInspectionItemRelationService {

    @Autowired
    private  ProcessInspectionItemRelationRepository processInspectionItemRelationRepository;
    @Autowired
    private  ProcessInspectionItemRelationMapper processInspectionItemRelationMapper;
    @Autowired
    private  ProcessInspectionItemRelationMapperEx processInspectionItemRelationExMapper;

    @Override
    public Page<ProcessInspectionItemRelationEx> getProcessInspectionItemRelationByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<ProcessInspectionItemRelation> page = processInspectionItemRelationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(ProcessInspectionItemRelation_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(ProcessInspectionItemRelation_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<ProcessInspectionItemRelationEx> page1 = page.map(processInspectionItemRelationExMapper::toEntity);
            return page1;

    }

    @Override
    public void addProcessInspectionItemRelation(ProcessInspectionItemRelationDto processInspectionItemRelationDto) {
            ProcessInspectionItemRelation result = processInspectionItemRelationRepository.findProcessInspectionItemRelationByIdAndDelete(processInspectionItemRelationDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("ProcessInspectionItemRelation不存在,请重新查询");
            }
            ProcessInspectionItemRelation processInspectionItemRelation = processInspectionItemRelationMapper.toEntity(processInspectionItemRelationDto);
            processInspectionItemRelationRepository.save(processInspectionItemRelation);
    }

    @Override
    public void updateProcessInspectionItemRelation(ProcessInspectionItemRelationDto processInspectionItemRelationDto) {
            ProcessInspectionItemRelation processInspectionItemRelation = processInspectionItemRelationRepository.findProcessInspectionItemRelationById(processInspectionItemRelationDto.getId());
            processInspectionItemRelation = processInspectionItemRelationMapper.toEntity(processInspectionItemRelationDto);
            processInspectionItemRelationRepository.save(processInspectionItemRelation);
    }


    @Override
    public void delProcessInspectionItemRelation(String id) {
        ProcessInspectionItemRelation processInspectionItemRelation = processInspectionItemRelationRepository.findProcessInspectionItemRelationById(id);
        processInspectionItemRelation.setDelete(1);
        processInspectionItemRelationRepository.save(processInspectionItemRelation);
    }

    @Override
    public void delProcessInspectionItemRelations (List<String> ids) { }
}